<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Ionize, creative CMS
 *
 * @package		Ionize
 * @author		Ionize Dev Team
 * @license		http://ionizecms.com/doc-license
 * @link		http://ionizecms.com
 * @since		Version 0.9.0
 */

// ------------------------------------------------------------------------

/**
 * Ionize, creative CMS Article Controller
 *
 * @package		Ionize
 * @subpackage	Controllers
 * @category	Article
 * @author		Ionize Dev Team
 *
 */
 
class pr_product extends MY_admin 
{

	/**
	 * Fields on wich the htmlspecialchars function will not be used before saving
	 * 
	 * @var array
	 */
	/**
	 * Fields on wich no XSS filtering is done
	 * 
	 * @var array
	 */
	/**
	 * Constructor
	 *
	 */
	public function __construct()
	{
		parent::__construct();
		// product model
		$this->load->model('pr_product_model', '', true);
		$this->load->model('pr_pdf_model', '', true);
	}

	// ------------------------------------------------------------------------
	/**
	 * Default : Do nothing
	 *
	 */
	function index()
	{
		//point to pr_product view, then pr_product point to pr_product_list
		$this->output('pr_product');
	}
	
	// get List product
	function get_list_pagination($page=1)
	{
		//$branchs	= $this->sibilling_model->get_by_field('branchs', 'id', $this->session->userdata('branchs_id'));
		if($this->input->post('num') != ''){
			$num = $this->input->post('num');
			$nb = ($this->input->post('nb')) ? $this->input->post('nb') : $num;
		}else{
			$num = 25;
			$nb = ($this->input->post('nb')) ? $this->input->post('nb') : $num;
		}
		
		$txt_filter = $this->input->post('keyword');
		
		if($txt_filter == ''){
			$keyword = '';
			$this->template['keyword'] = '';
		}else{
			$num = 25;
			$this->template['keyword'] = $txt_filter;
			$keyword = "WHERE lower(pd_name) like '%".strtolower($txt_filter)."%'";
		}
		//trace($txt_filter);
		// Minimum
		if ($nb < $num)	$nb = $num;
		
		$page 	= $page - 1;
		$offset = $page * $nb;
		
		$sql	= 	"SELECT * FROM tbl_product ".$keyword."
					ORDER BY pd_id DESC
					LIMIT ".$nb. " OFFSET ".$offset ;
			
		//trace($sql);				
		
		$products = $this->pr_product_model->get_by_sql($sql);
		$this->template['products'] = $products;
		//trace($odf_list);
		
		// Pagination
		$this->template['current_page'] = $page + 1;
		$this->template['nb'] = $nb;
		$this->template['option'] = $num;
		$this->template['port_list_count'] = $this->pr_product_model->count_by_sql("SELECT * FROM tbl_product ORDER BY pd_id DESC");
		
		$this->template['port_list_pages'] = ceil($this->template['port_list_count'] / $nb);
		$this->template['paginations']=$this->connect->paginations($this->template['port_list_pages'],$this->template['current_page']);
		
		$this->output('pr_product_list');
	}
	
	/**
	 *  Generad PDF file
	 *
	 */	
	function pdf($index,$bank = false)
	{
		$this->load->library('fpdf');
		$pdf = new FPDF();
			
		$data = array(
							'pd_id'		  		=> $index
						 );	
						 
		// Get invoices 
		$get_head	=	$this->pr_pdf_model->PDF_invoice_body($data,'pd_id','ASC');
		$pdf->pd_info = $get_head;	
		
		$pdf->AliasNbPages();	
		$pdf->AddPage();
		$pdf->SetAutoPageBreak(true,85);
			/*
			**	Body	
			*/ 
			$title 	= array('ID', 'Cat_Name', 'Pd_Name', 'Pd_Price', 'Pd_Des', 'Pd_Image');
			
			/*$data = array(
							'pd_id'		  		=> $index
						 );	*/
						 
			// Get invoices 
			$get_invoices	=	$this->pr_pdf_model->PDF_invoice_body($data,'pd_id','ASC');
			
			//$pdf->pd_info = $get_invoices;					
			//trace($pdf->pd_info);exit;
			
			$pdf->body($title,$get_invoices);
			
		// Generate pdf file
		$pdf->Output(FCPATH.'/files/'.$get_invoices[0]['pd_id'].'.pdf');	
		
		// Open pdf file on pop-up window
		echo '
			<script>
				window.open("'.base_url().'/files/'.$get_invoices[0]['pd_id'].'.pdf'.'","Inward PDF","width=850,height=700")
        	</script>
		';	
	}//end function
	
	function product_filter()
	{
		//point to pr_product view, then pr_product point to pr_product_list
		$this->output('pr_product_filter');
	}
	// get List product
	function get_list_filter($page=1)
	{
		$nb = ($this->input->post('nb')) ? $this->input->post('nb') : '25';
		
		// Minimum
		if ($nb < 25)	$nb = 25;
	
		$page 	= $page - 1;		
		$offset = $page * $nb;
		
		if(count($this->input->post()) > 1)
			$posts	= $this->input->post();
		else
			$posts	= array('pd_name' => '');
		
		//$posts['branch_id']	= $this->current_user['branchs_id'];		

		$this->template['products']	= $this->pr_product_model->get_by_filter('tbl_product', $posts, 'pd_id', 'DESC', $nb, $offset);
				
		//trace($offset);
		
		// Pagination
		$this->template['current_page'] = $page + 1;
		$this->template['nb'] = $nb;
		$this->template['option'] = $nb;
		$this->template['port_list_count'] = $this->pr_product_model->count_by_sql("SELECT * FROM tbl_product ORDER BY pd_id DESC");
		
		$this->template['port_list_pages'] = ceil($this->template['port_list_count'] / $nb);
		$this->template['paginations']=$this->connect->paginations($this->template['port_list_pages'],$this->template['current_page']);
		
		$this->output('pr_product_list_filter');
	}
	
	function page_product_new(){		
		$this->output('pr_page_product_new');
	}
	function page_insert_product()
	{
		if ($this->input->post('pd_name') != '' )
		{
				$data = array(		
							  'pd_name'		=> $this->input->post('pd_name'),	
							  'pd_price'			=> $this->input->post('pd_price'),
							  'pd_des'	=> $this->input->post('pd_des'),						  	
							  'pd_image'	=> $this->input->post('picture'),
							  );
				// Insert
				$affected_rows = $this->pr_product_model->insert('tbl_product',$data);

				if ($affected_rows > 0)
				{
					// UI update panels
					$this->update[] = array(
						'element' => 'pr_product_list',
						'url' => admin_url() . 'pr_product/get_list_pagination'
					 );
					 // Success message
					$this->success(lang('pr_message_new'));
				} 
		}//end if check textbox can't null
		else
		{
			$this->error(lang('pr_error_submit_form'));
		}
	}//end function
	
	//show data to edit
	function page_edit_product($id){
		$product_data	= $this->pr_product_model->get_by_sql("SELECT * FROM tbl_product WHERE pd_id=".$id);			
		$this->template['product_data']	= $product_data;		
		$this->output('pr_page_edit_product');
	}
	function page_update_product($pd_id)
	{					
		$data = array(
				'pd_name'=> $this->input->post('pd_name'),
				'pd_price'=> $this->input->post('pd_price'),
				'pd_des'=> $this->input->post('pd_des'),
				'pd_image'=> $this->input->post('picture')			
				 );

				$affected_rows = $this->pr_product_model->save_edit_product($data,$pd_id);
				///trace($affected_rows);
				if ($affected_rows > 0)
				{
					// UI update panels
					$this->update[] = array(
						'element' => 'pr_product_list',
						'url' => admin_url() . 'pr_product/get_list_pagination'
					 );
					 
					// Success message
					$this->success(lang('pr_message_new'));
				}
				else
				{
					$this->error('Fail!..');
				}
	}//end function
	
	/// function remove POP / POLL location
	function delete_product($id){
		$data = array( 'pd_id' => $id);
		$affected_rows = $this->pr_product_model->delete('tbl_product',$data);

		if ($affected_rows > 0)
		{
			// UI update panels
			$this->update[] = array(
				'element' => 'pr_product_list',
				'url' => admin_url() . 'pr_product/get_list_pagination'
			);
			$this->success('Successful!..');
		}
		else
		{
			$this->error('Fail!..');
		}	
	}
	/**
	 * view_picture
	 *
	 */
	function view_picture($picture=NULL, $zoom=NULL)
	{

		if($zoom == NULL)
			echo '<img width="115px" height="115px" src="'.base_url().'files/pictures/'.$picture.'" /><input type="hidden" id="picture_name" value="'.$picture.'" />';
		else
			echo '<img width="100%" height="100%" src="'.base_url().'files/pictures/'.$picture.'" />';
	}
	
	/**
	 * upload_file
	 *
	 */
	function upload_file()
	{		
		//////////////////////File//////////////////////////////////
		$file_element_name = 'userfile';
		
		$this->gallery_path = str_replace('\\','/',realpath(APPPATH. '../files/pictures'));
		$config = array(
										//'file_name'    	=> time().'_'.'New Name',//pic name
										'allowed_types' 	=> 'jpeg|gif|jpg|png|bmp',
										'upload_path' 		=> $this->gallery_path,
										'max_size' 			=> 5120
									);
		
		$this->load->library('upload', $config);
	
		if(!$this->upload->do_upload($file_element_name)){
			echo 'Upload fail';
		}
		else
		{
			$image_data = $this->upload->data();
			echo $image_data['file_name'];
		}
	}
	
	/**
	 * Delete file
	 *
	 */
	function delete_file()
	{
		if ($this->input->post('picture')	!= '')
		{
			$this->gallery_path = str_replace('\\','/',realpath(APPPATH. '../files/pictures'));
			unlink($this->gallery_path.'/'.$this->input->post('picture'));
		}
	}	
}


/* End of file article.php */
/* Location: ./application/controllers/admin/article.php */